package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employee
     * @param httpSession
     * @return
     */
    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession httpSession) {

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        if (emp == null) {
            return R.error("用户名不存在");
        }

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        if (emp.getStatus() == 0) {
            return R.error("员工已禁用");
        }

        httpSession.setAttribute("employee", emp.getId());
        emp.setPassword(null);
        return R.success(emp);
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("");
    }

    /**
     * 新增员工
     * @param employee
     * @param httpSession
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee,HttpSession httpSession) {
        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes()));
       // employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

       // Employee emp = (Employee) httpSession.getAttribute("employee");
       // Long id = emp.getId();
        //employee.setCreateUser(id);
       // employee.setUpdateUser(id);

        boolean save = employeeService.save(employee);
        return save?R.success("保存成功"):R.error("添加失败");
    }

    /**
     * 员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Map> page( int page, int pageSize,String name){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(name != null,Employee::getUsername,name);
         employeeService.page(pageInfo, qw);


        long total = pageInfo.getTotal();
        List records = pageInfo.getRecords();

        HashMap map = new HashMap();
        map.put("records",records);
        map.put("total",total);
        return R.success(map);
    }

    /**
     * 员工的禁用和启用
     * @param employee
     * @param httpSession
     * @return
     */
    @PutMapping
    public R<Employee> update(@RequestBody Employee employee,HttpSession httpSession){
        //Employee emp = (Employee) httpSession.getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(emp.getId());

        LambdaQueryWrapper<Employee> eq = new LambdaQueryWrapper<>();
        eq.eq(Employee::getId,employee.getId());

        boolean flag = employeeService.update(employee,eq);
        return flag?R.success(employee):R.error("修改失败");
    }

    /**
     * 编辑员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id){
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getId,id);
        Employee one = employeeService.getOne(qw);
        return one!=null?R.success(one):R.error("没有查询到对应员工信息");
    }

}
